<%
=begin
* need to begin restricting. don't display empty form for full associations. optionally don't allow already-associated objects to be reassociated.

TODO
don't display "create" without create permissions
don't display existing without update permissions (or rather, just use ui_type => :select)
only display one block for singular associations
post multiple new records
contextual error display (tough)
build finder
  * it's a simplified list. it doesn't accept action links. it has searching and pagination.
  * it can be configured with a discrete list of columns.
  * its purpose is to pass a record id to some callback, via ruby or javascript.
  * the callback in ruby would be an overridable method called "with_found(record)".
  * if called in a redbox popup, javascript would be able to intercept the find event.
  * it would use list permissions.

=end
%>
<%
parent_record = @record
associated = parent_record.send(column.name).sort_by {|r| r.id}
-%>
<label><%= column.label -%></label>
<ul class="association">

  <% associated.each do |record| %>
  <% @record = record -%>
  <li class="association-record">
    <%= render :partial => 'form_association_record', :locals => {:scope => "[#{column.name}][#{@record.id}]", :parent_record => parent_record} %>
  </li>
  <% end -%>

  <li>
    <%
    @record = column.association.klass.new
    template = render(:partial => 'form_association_record', :locals => {:scope => "[#{column.name}][--new--]", :parent_record => parent_record})
    template = h(template.gsub(/[\n\r]/, '').gsub(/'/, "&#39;"))
    -%>
    <%= link_to_function 'Create Another', %|t='<li class="association-record association-record-new">#{template}</li>'; new Insertion.Before(this.parentNode, t.gsub(/--new--/, 'new' + Math.floor(Math.random() * 10000000)))| %>
    | <a>Find Existing</a>
  </li>
</ul>
<% @record = parent_record -%>